package common.store.web.controller;



import common.store.admin.dto.Category;
import common.store.admin.service.CategoryService;
import common.store.admin.service.impl.CategoryServiceImpl;
import common.store.web.bean.CartAndProduct;
import common.store.web.service.CartService;
import common.store.web.service.impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/cartList")
public class CartListServlet extends HttpServlet {

    CartService cartService;
    private CategoryService categoryService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        cartService = new CartServiceImpl();
        categoryService = new CategoryServiceImpl();

        List<Category> categoryList = categoryService.getCategoryList();
        String uid = req.getParameter("uid");
        if(uid==null || uid=="") req.getRequestDispatcher("/login.jsp").forward(req , resp);

        List<CartAndProduct> cartList = cartService.getCartList(uid);
        Double sum=0.0D;
        for (CartAndProduct cart: cartList ) {
            sum+=cart.getCount()*cart.getShopPrice();
        }
        req.setAttribute("categoryList" , categoryList);
        req.setAttribute("cartList" , cartList);
        req.setAttribute("cartMoney" , sum);
            req.getRequestDispatcher("/cart.jsp").forward(req , resp);
            req.getRequestDispatcher("/order_list.jsp").forward(req , resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req , resp);
    }
}
